/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MConfigInterface.h"

//---------------------------------------------------------------------------

MConfigInterface::MConfigInterface(void) : MFileConfig()
{
	Initialize();

	applicationPath.Empty();
}

MConfigInterface::~MConfigInterface(void)
{
}

//---------------------------------------------------------------------------

void MConfigInterface::Initialize(void)
{
	interfaceName.Empty();
}

void MConfigInterface::LoadFromPointer(MConfigInterface *dataIn)
{
	Initialize();
	if(dataIn==NULL) return;

	interfaceName = dataIn->interfaceName;
}

//---------------------------------------------------------------------------

CString MConfigInterface::BuildTargetFileName(void)
{
	CString str;
	TCHAR *buff;
	DWORD size;
	DWORD err;

	str.Empty();
	if(applicationPath.IsEmpty())
		return str;

	buff = NULL;
	size = 0;
	GetComputerNameEx(ComputerNamePhysicalDnsHostname, buff, &size);
	err = GetLastError();
	if(err!=ERROR_MORE_DATA)
		return str;

	try{
		buff = new TCHAR[size+1];
	}
	catch(...){
		buff = NULL;
	}
	if(buff==NULL) return str;

	memset(buff, 0, (size+1) * sizeof(TCHAR));
	if(!GetComputerNameEx(ComputerNamePhysicalDnsHostname, buff, &size)){
		delete[] buff;
		return str;
	}

	str  = applicationPath;
	str += buff;

	delete[] buff;

	return str;
}

//---------------------------------------------------------------------------

bool MConfigInterface::LoadFromFile(void)
{
	CString targetFileName;

	Initialize();

	targetFileName = BuildTargetFileName();
	if(targetFileName.IsEmpty())
		return false;

	return MFileConfig::LoadFromFile(targetFileName);
}

bool MConfigInterface::SaveToFile(void)
{
	CString targetFileName;

	targetFileName = BuildTargetFileName();
	if(targetFileName.IsEmpty())
		return false;

	return MFileConfig::SaveToFile(targetFileName);
}

//---------------------------------------------------------------------------

CString MConfigInterface::BuildId(CString strIn)
{
	CString str;

	str = strIn;
	while(str.GetLength()<22)
		str += _T(" ");
	str += _T(" = ");

	return str;
}

//---------------------------------------------------------------------------

void MConfigInterface::InternalInit(void)
{
	Initialize();
}

//---------------------------------------------------------------------------

bool MConfigInterface::InternalProcessLine(char* buf)
{
	CString str, strId;
	int pos;

	if(buf==NULL){
		// nothing to process
		return true;
	}

	str = buf;
	pos = str.Find('=');
	if(pos==-1){
		// invalid line
		return false;
	}

	strId = str.Left(pos);
	strId = strId.Trim();

	str = str.Mid(pos+1);
	str = str.Trim();

	if(strId.CompareNoCase("DefaultInterface")==0){
		interfaceName = str;
		return true;
	}

	// unknown directive

	return true;
}

//---------------------------------------------------------------------------

bool MConfigInterface::InternalSave(void)
{
	CString str;

	if(!WriteEmptyCommentLine()) return false;
	if(!WriteCommentLine(_T("CHScanner - Interface Configuration File"))) return false;
	if(!WriteEmptyCommentLine()) return false;

	if(!WriteEmptyLine()) return false;

	if(interfaceName.IsEmpty())
		return true;

	if(!WriteLineWithId(_T("DefaultInterface"), interfaceName)) return false;

	return true;
}

//---------------------------------------------------------------------------
